package com.meiyou.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meiyou.service.AliyunVideoService;
import com.meiyou.utils.Context;
import com.meiyou.utils.Msg;
import com.meiyou.utils.ShareCodeUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value = "阿里云音视频通讯调用控制器", tags = { "支付登录接口" })
public class AliyunVideosController {
    
	@Autowired
	AliyunVideoService aliyunVideoService;
	
	
	
	/**
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value="videoChat",method=RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "视频通话")
	public Msg videoChat(String toUccount, HttpServletRequest req) {
		if(req.getSession().getAttribute("user")!=null) {
			return aliyunVideoService.videoChat(req.getSession().getAttribute("user").toString(), toUccount);
		}else {
			Msg msg =  Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	
	}
	
	
	
	
	
	
	/**
	 * 获取鉴权令牌
	 * @param req
	 * @return
	 */
	@RequestMapping(value="getToken",method=RequestMethod.POST)
	@ResponseBody
	public Msg getToken(String channelID,String channelKey,String nonce,long timstamp, HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			msg = Msg.fail();
			msg.setMsg("请先登录");
		    return msg;
		}else {
			 String ucount = req.getSession().getAttribute("user").toString();
			 String token = aliyunVideoService.createToken(Context.VIDEOAPPID, ucount, channelID, channelKey, nonce, timstamp);
             if(token!=null) {
            	 Map<String,Object> map = new HashMap<String,Object>();
                 map.put("token", token);
    			 msg = Msg.success();
    			 msg.setExtend(map);
    			 return msg;
             }else {
            	 msg = Msg.fail();
            	 msg.setMsg("获取token失败");
            	 return msg;
             }
			 
		}
	}
	
	
	
	
	
	

	/**
	 * 创建聊天频道
	 * @param req
	 * @return
	 */
	@RequestMapping(value="createChanel",method=RequestMethod.POST)
	@ResponseBody
	public Msg createChanel(String uccount,HttpServletRequest req) {
		Msg msg;
		if(req.getSession().getAttribute("user")==null) {
			msg = Msg.fail();
			msg.setMsg("请先登录");
		    return msg;
		}else {
			if(aliyunVideoService.createChannel(Context.VIDEOAPPID, ShareCodeUtil.getRamdomCount()).getCode()==100) {
				msg = Msg.success();
				
				return msg;
			}else {
				msg = Msg.fail();
				msg.setMsg("创建频道失败");
				return msg;
			}
		}
	}
}
